package com.lefu.es.db1;

import java.util.Date;

import android.text.TextUtils;

import com.lefu.es.application.IwellnessApplication;
import com.lefu.es.constant.UtilConstants;
import com.lefu.es.entity.NutrientBo;
import com.lefu.es.entity.Records;
import com.lefu.es.entity.UserModel;
import com.lefu.es.service.RecordService;
import com.lefu.es.service.UserService;
import com.lefu.es.util.MyUtil;
import com.lefu.es.util.StringUtils;
import com.lefu.es.util.Tool;
import com.lefu.es.util.UtilTooth;

/**
 * 测量记录
 * @author lfl
 */
public class RecordDao {
	
	/**处理数据*/
	public static void dueDate(RecordService recordService,String readMessage) {
		Records recod = MyUtil.parseMessage(recordService, readMessage);
		handleData(recordService,recod,readMessage);
	}
	
	/**处理数据*/
	public static void handleData(RecordService recordService,Records recod,String readMessage){
		if (recod.getScaleType().equalsIgnoreCase(UtilConstants.BODY_SCALE)) {
			try {
				UserModel user = UtilConstants.CURRENT_USER;
//				UserModel um = new UserModel();
//				um.setId(user.getId());
//				um.setSex(recod.getSex());
//				um.setBheigth(Float.parseFloat(recod.getsHeight()));
//				um.setLevel(recod.getLevel());
//				um.setAgeYear(Integer.parseInt(recod.getsAge()));
//				um.setDanwei(user.getDanwei());
				recod.setUseId(user.getId());
				recod.setUgroup(user.getGroup());
				recod.setRbmr(StringUtils.isNumber(recod.getSbmr()) == true ? (Integer.parseInt(recod.getSbmr()) / 1) : 0);
				recod.setRbodyfat(StringUtils.isNumber(recod.getSbodyfat()) == true ? Float.parseFloat(recod.getSbodyfat()) : 0);
				recod.setRbodywater(StringUtils.isNumber(recod.getSbodywater()) == true ? Float.parseFloat(recod.getSbodywater()) : 0);
				recod.setRbone(StringUtils.isNumber(recod.getSbone()) == true ? Float.parseFloat(recod.getSbone()) : 0);
				recod.setRmuscle(StringUtils.isNumber(recod.getSmuscle()) == true ? Float.parseFloat(recod.getSmuscle()) : 0);
				recod.setRvisceralfat(StringUtils.isNumber(recod.getSvisceralfat()) == true ? (Integer.parseInt(recod.getSvisceralfat()) / 1) : 0);
				recod.setRweight(StringUtils.isNumber(recod.getSweight()) == true ? Float.parseFloat(recod.getSweight()) : 0);
				recod.setRecordTime(UtilTooth.dateTimeChange(new Date()));
				if (UtilConstants.BABY_SCALE.equals(recod.getScaleType())) {
					recod.setRweight(UtilTooth.myround2((float) (recod.getRweight() * 0.1)));
				} else {
					recod.setRweight(UtilTooth.myround((float) (recod.getRweight())));
				}
				if (StringUtils.isNumber(recod.getSbmi())) {
					recod.setRbmi(UtilTooth.myround(UtilTooth.countBMI2(recod.getRweight(), (Float.parseFloat(recod.getsHeight())) / 100)));
				}
				if (StringUtils.isNumber(recod.getsHeight()) == true && !"0".equals(recod.getsHeight())) {
					recod.setSbmi(String.valueOf(recod.getRbmi()));
				}
//				if (StringUtils.isNumber(recod.getSbmi())) {
//					recod.setRbmi(UtilTooth.myround(UtilTooth.countBMI2(recod.getRweight(), (Float.parseFloat(recod.getsHeight())) / 100)));
//				}
				try {
					Records lastRecod = recordService.findLastRecordsByScaleTypeAndUser(recod.getScaleType(), String.valueOf(user.getId()));
					if (null != lastRecod) {
						recod.setCompareRecord((UtilTooth.myround(recod.getRweight() - lastRecod.getRweight())) + "");
						recod.setComBodyWater(UtilTooth.myround(recod.getRbodywater() - lastRecod.getRbodywater()));
						recod.setComBodyFat(UtilTooth.myround(recod.getRbodyfat() - lastRecod.getRbodyfat()));
						recod.setComBone(UtilTooth.myround(recod.getRbone()-lastRecod.getRbone()));
						recod.setComBmi(UtilTooth.myround(recod.getRbmi()-lastRecod.getRbmi()));
						recod.setComVisFat(UtilTooth.myround(recod.getRvisceralfat() - lastRecod.getRvisceralfat()));
						recod.setComBmr(UtilTooth.myround(recod.getRbmr()-lastRecod.getRbmr()));
						recod.setComMuscle(UtilTooth.myround(recod.getRmuscle()-lastRecod.getRmuscle()));
						recod.setComAge(UtilTooth.myround(recod.getBodyAge() - lastRecod.getBodyAge()));

					} else {
						recod.setCompareRecord("0.0");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				recordService.saveOneRecord(recod);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			UserModel user = UtilConstants.CURRENT_USER;
			recod = new Records();
			recod.setUseId(user.getId());
			recod.setScaleType(readMessage.substring(0, 2));
			recod.setUgroup("P" + StringUtils.hexToTen(readMessage.substring(3, 4)) + "");
			recod.setLevel(StringUtils.hexToTen(readMessage.substring(2, 3)) + "");
			String biary = StringUtils.hexToBirary(readMessage.substring(4, 6));
			if (biary.length() < 8) {
				for (int i = biary.length(); i < 8; i++) {
					biary = "0" + biary;
				}
			}
			recod.setSex(biary.substring(0, 1));
			recod.setsAge(StringUtils.binaryToTen(biary.substring(1)) + "");
			recod.setsHeight(StringUtils.hexToTen(readMessage.substring(6, 8)) + "");
			recod.setSweight((StringUtils.hexToTen(readMessage.substring(8, 12)) * 0.1) + "");
			recod.setSbodyfat((StringUtils.hexToTen(readMessage.substring(12, 16)) * 0.1) + "");
			recod.setSbone((StringUtils.hexToTen(readMessage.substring(16, 18)) * 0.1) + "");
			recod.setSmuscle((StringUtils.hexToTen(readMessage.substring(18, 22)) * 0.1) + "");
			recod.setSvisceralfat(StringUtils.hexToTen(readMessage.substring(22, 24)) + "");
			recod.setSbodywater((StringUtils.hexToTen(readMessage.substring(24, 28)) * 0.1) + "");
			recod.setSbmr((StringUtils.hexToTen(readMessage.substring(28, 32)) * 1) + "");

			recod.setRbmr(StringUtils.isNumber(recod.getSbmr()) == true ? (Integer.parseInt(recod.getSbmr()) / 1) : 0);
			recod.setRbodyfat(StringUtils.isNumber(recod.getSbodyfat()) == true ? Float.parseFloat(recod.getSbodyfat()) : 0);
			recod.setRbodywater(StringUtils.isNumber(recod.getSbodywater()) == true ? Float.parseFloat(recod.getSbodywater()) : 0);
			recod.setRbone(StringUtils.isNumber(recod.getSbone()) == true ? Float.parseFloat(recod.getSbone()) : 0);
			recod.setRmuscle(StringUtils.isNumber(recod.getSmuscle()) == true ? Float.parseFloat(recod.getSmuscle()) : 0);
			recod.setRvisceralfat(StringUtils.isNumber(recod.getSvisceralfat()) == true ? (Integer.parseInt(recod.getSvisceralfat()) / 1) : 0);
			recod.setRweight(StringUtils.isNumber(recod.getSweight()) == true ? Float.parseFloat(recod.getSweight()) : 0);
			recod.setRecordTime(UtilTooth.dateTimeChange(new Date()));
			if (UtilConstants.BABY_SCALE.equals(recod.getScaleType())) {
				recod.setRweight(UtilTooth.myround2((float) (recod.getRweight() * 0.1)));
			} else {
				recod.setRweight(UtilTooth.myround((float) (recod.getRweight())));
			}
			if (StringUtils.isNumber(recod.getsHeight()) == true && !"0".equals(recod.getsHeight())) {
				recod.setSbmi(UtilTooth.countBMI(recod.getRweight(), (Float.parseFloat(recod.getsHeight())) / 100));
			}
			if (StringUtils.isNumber(recod.getSbmi())) {
				recod.setRbmi(UtilTooth.myround(UtilTooth.countBMI2(recod.getRweight(), (Float.parseFloat(recod.getsHeight())) / 100)));
			}
			if (StringUtils.isNumber(recod.getsHeight()) == true && !"0".equals(recod.getsHeight())) {
				recod.setSbmi(String.valueOf(recod.getRbmi()));
			}
			try {
				Records lastRecod = recordService.findLastRecordsByScaleTypeAndUser(recod.getScaleType(), String.valueOf(user.getId()));
				if (null != lastRecod) {
					recod.setCompareRecord((UtilTooth.myround(recod.getRweight() - lastRecod.getRweight())) + "");
					recod.setComBodyWater(UtilTooth.myround(recod.getRbodywater() - lastRecod.getRbodywater()));
					recod.setComBodyFat(UtilTooth.myround(recod.getRbodyfat() - lastRecod.getRbodyfat()));
					recod.setComBone(UtilTooth.myround(recod.getRbone()-lastRecod.getRbone()));
					recod.setComBmi(UtilTooth.myround(recod.getRbmi()-lastRecod.getRbmi()));
					recod.setComVisFat(UtilTooth.myround(recod.getRvisceralfat() - lastRecod.getRvisceralfat()));
					recod.setComBmr(UtilTooth.myround(recod.getRbmr()-lastRecod.getRbmr()));
					recod.setComMuscle(UtilTooth.myround(recod.getRmuscle()-lastRecod.getRmuscle()));
					recod.setComAge(UtilTooth.myround(recod.getBodyAge() - lastRecod.getBodyAge()));
				} else {
					recod.setCompareRecord("0.0");
				}
				recordService.saveOneRecord(recod);
			} catch (Exception e) {
			}
		}
	}
	
	private static UserService uservice;
	public static void dueKitchenDate(RecordService recordService,String readMessage,NutrientBo nutrient) {
		Records recod = MyUtil.parseMessage(recordService, readMessage);
		handleKitchenData(recordService,recod,nutrient);
	}
	
	public static Records handleKitchenData(RecordService recordService,Records recod,NutrientBo nutrient){
		if(null!=recod && null!=recordService){
			uservice = new UserService(IwellnessApplication.app);
			UserModel user = UtilConstants.CURRENT_USER;
			if(null!=user){
				if(recod.getUnitType()==0){
					user.setDanwei(UtilConstants.UNIT_G);
				}else if(recod.getUnitType()==1){
					user.setDanwei(UtilConstants.UNIT_LB);
				}else if(recod.getUnitType()==2){
					user.setDanwei(UtilConstants.UNIT_ML);
				}else if(recod.getUnitType()==3){
					user.setDanwei(UtilConstants.UNIT_FATLB);
				}
				recod.setUseId(user.getId());
				recod.setUgroup(user.getGroup());
			}
			recod.setScaleType(UtilConstants.KITCHEN_SCALE);
			recod.setRecordTime(UtilTooth.dateTimeChange(new Date()));
			if(null!=nutrient){
				float calcium =  0.0f;
				if(!TextUtils.isEmpty(nutrient.getNutrientCalcium()) && Tool.isDigitsOnly(nutrient.getNutrientCalcium())){
					calcium =  Float.parseFloat(nutrient.getNutrientCalcium())*recod.getRweight()*0.01f;//钙
				}
				float protein =  0.0f;
				if(!TextUtils.isEmpty(nutrient.getNutrientProtein()) && Tool.isDigitsOnly(nutrient.getNutrientProtein())){
					protein = Float.parseFloat(nutrient.getNutrientProtein())*recod.getRweight()*0.01f;//蛋白质
				}
				float kcal = 0.0f;
				if(!TextUtils.isEmpty(nutrient.getNutrientEnerg()) && Tool.isDigitsOnly(nutrient.getNutrientEnerg())){
					kcal =  Float.parseFloat(nutrient.getNutrientEnerg())*recod.getRweight()*0.01f;//卡路里
				}
				float water =  0.0f;
				if(!TextUtils.isEmpty(nutrient.getNutrientCarbohydrt()) && Tool.isDigitsOnly(nutrient.getNutrientCarbohydrt())){
					water =  Float.parseFloat(nutrient.getNutrientCarbohydrt())*recod.getRweight()*0.01f;//碳水化合物
				}
				float fat =  0.0f;
				if(!TextUtils.isEmpty(nutrient.getNutrientLipidTot()) && Tool.isDigitsOnly(nutrient.getNutrientLipidTot())){
					fat =  Float.parseFloat(nutrient.getNutrientLipidTot())*recod.getRweight()*0.01f;//脂肪
				}
				float fibre =  0.0f;
				if(!TextUtils.isEmpty(nutrient.getNutrientFiberTD()) && Tool.isDigitsOnly(nutrient.getNutrientFiberTD())){
					fibre =  Float.parseFloat(nutrient.getNutrientFiberTD())*recod.getRweight()*0.01f;//纤维
				}
				float cholesterol =  0.0f;
				if(!TextUtils.isEmpty(nutrient.getNutrientCholestrl()) && Tool.isDigitsOnly(nutrient.getNutrientCholestrl())){
					cholesterol =  Float.parseFloat(nutrient.getNutrientCholestrl())*recod.getRweight()*0.01f;//胆固醇
				}
				float vitaminC =  0.0f;
				if(!TextUtils.isEmpty(nutrient.getNutrientVitB6()) && Tool.isDigitsOnly(nutrient.getNutrientVitB6())){
					vitaminC =  Float.parseFloat(nutrient.getNutrientVitB6())*recod.getRweight()*0.01f;//维生素C
				}
				
				recod.setRphoto(nutrient.getNutrientDesc());
				recod.setRbodywater(kcal) ; //卡路里
				recod.setRbodyfat(protein);//蛋白质
				recod.setRbone(fat); //脂肪
				recod.setRmuscle(water);//碳水化合物
				recod.setRvisceralfat(fibre) ;//纤维
				recod.setRbmi(cholesterol);//胆固醇
				recod.setRbmr(vitaminC) ;//维生素C
				recod.setBodyAge(calcium);//钙
			}else{
				recod.setRbodywater(0.0f) ; //卡路里
				recod.setRbodyfat(0.0f);//蛋白质
				recod.setRbone(0.0f); //脂肪
				recod.setRmuscle(0.0f);//碳水化合物
				recod.setRvisceralfat(0.0f) ;//纤维
				recod.setRbmi(0.0f);//胆固醇
				recod.setRbmr(0.0f) ;//维生素C
				recod.setBodyAge(0.0f);//钙
			}
			
			try {
				if(null!=user && null!=uservice){
					uservice.update(user);
				}
				recordService.saveOneRecord(recod);
			} catch (Exception e) {
			}
		}
		
		return recod;
	}
}
